package com.aispeech.task.lasr.event;

import com.aispeech.task.common.data.domain.Task;
import com.aispeech.task.common.exceptions.TaskException;
import com.aispeech.task.constant.LASRProgress;
import com.aispeech.task.fsm.Event;
import com.aispeech.task.fsm.TerminalEvent;
import com.aispeech.task.lasr.LASREventType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import static com.aispeech.task.lasr.LASRTaskHandler.updateTaskStatus;

@Slf4j
public class LabelEvent extends Event<Task> {
    public LabelEvent(Task task) {
        super(LASREventType.FusedAndNeedLabel, task);
    }

    @Override
    public Event fire() {
        Task task = this.getTarget();

        Event nextEvent;

        try {
            updateTaskStatus(task, LASRProgress.LABEL, "ts_label");

            ObjectMapper mapper = new ObjectMapper();
            log.info("[ LabelEvent.fire ] detail: {}", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(task));

            nextEvent = new TerminalEvent(task);
        } catch (TaskException | JsonProcessingException ex) {
            nextEvent = new ErrorEvent(task);
        }

        return nextEvent;
    }
}
